package cn.edu.nju.ws.sview.views;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;

/**
 * 
 * @author ssgong
 *
 */
public class URIDataMember extends DataMember{
    private String uri;
    private int id;
    private boolean inverse;
    private int valueType=-1;
    private boolean functional;
    
    public URIDataMember(String uri){
    	this.uri = uri;
    	this.inverse = false;
    	this.valueType =-1;
    }
    
    public String getURI(){
    	return this.uri;
    }
    
    public int getValueType(boolean inverse){
    	if(this.valueType==-1){
    		this.valueType = getValueTypeByDMId(this.getID(),inverse);
    	}
    	return this.valueType;
    }
    
    public int getValueType(){
    	return this.valueType; 
    }
    
    public void setValueType(int valueType){
    	this.valueType = valueType;
    }
    
    public int getID(){
    	if(this.id ==0){
    		try {
				id = URIIDCache.getInstance().getURIID(uri, true) ;
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	return this.id;
    }
    
    public void setID(int id){
    	this.id = id;
    }
    
    public boolean isInverse(){
    	return this.inverse;
    }
    
    public boolean getFunctional(){
    	return this.functional;
    }
    
    public void setFunctional(boolean functional){
    	this.functional = functional;
    }
    
    public void setInverse(boolean inverse){
    	this.inverse = inverse;
    }
    
	/**
	 * 查询一个原始的uri属性是datatype property 还是URI property
	 * 
	 * !!!如果三元组表中没有一个关于该property的三元组，那么就假定是datavalue property
	 *    这个假定可能会出错
	 * !!!
	 * @param propertyID.即uri data member的id
	 * @return 返回值参见DataMember中的值
	 */
    public static int getValueTypeByDMId(int propertyID,boolean inverse){
		int result = 0;
		
		Connection con = DBConnectionFactory.getConnection();
		try {
			PreparedStatement stmt = null;
			String str = "u"+propertyID;
			if(!inverse)
				stmt = con.prepareStatement("select null from quadruple where p=? and o like 'u%' limit 1");
			else
				stmt = con.prepareStatement("select null from quadruple where p=? and s like 'u%' limit 1");
			con.createStatement();
			stmt.setString(1, str) ;
			ResultSet rs =stmt.executeQuery();
			if(rs.next()){
				result = DataMember.VALUE_TYPE_LINK;
			}else
				result = DataMember.VALUE_TYPE_ATTRIBUTE;
			rs.close();
			stmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
    }

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (inverse ? 1231 : 1237);
		result = prime * result + ((uri == null) ? 0 : uri.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		URIDataMember other = (URIDataMember) obj;
		if (inverse != other.inverse)
			return false;
		if (uri == null) {
			if (other.uri != null)
				return false;
		} else if (!uri.equals(other.uri))
			return false;
		return true;
	}
    
    
}
